import { observable, action } from 'mobx';
import { WeaForm } from 'comsMobx';

import * as Api from './../apis/accountListApi';


export default class AccountListStore {
  @observable showDropMenu = true;
  @observable groupid = 'Sent';// tab页签id
  @observable form = new WeaForm();
  @observable showSearchAd = false; // 高级搜搜form表单是否显示
  @observable searchForm = new WeaForm();// 搜索form
  @observable columns = [
    { title: ' ', dataIndex: 'name', key: 'name' },
  ];
  @observable dataSource = [];
  @observable childDatas = [];
  @observable current = 1;
  @observable count = 0;
  @observable pageSize = 10;
  @observable loading = false;

  // 控制高级搜索显隐
  @action
  setShowSearchAd = (bool) => {
    this.showSearchAd = bool;
  }

  // 获取表格列表
  @action.bound
  getTableList(searchValue) {
    const searchValueForm = this.searchForm.getFormParams();
    const params = {
      groupid: 'Sent', // tab ID
      Title: searchValueForm.Title || searchValue, // 公文标题
      shopName: searchValueForm.shopName || '', // 发文字号
      operationDate: searchValueForm.operationDate || '', // 发送时间或处理时间
      startDate: searchValueForm.startDate || '', // 开始时间
      endDate: searchValueForm.endDate || '', // 结束时间
      isShowReceiveDocInfo: 1,
    };

    Api.getTableList(params).then((data) => {
      if (data.sessionkey) {
        const param = {
          dataKey: data.sessionkey,
          current: 1,
        };
        let columns_display = [];
        let dataSource_display = [];

        this.getTableDatas(param, columns_display, dataSource_display);
        this.getTableCounts(param);
        this.showSearchAd = false;
      }
    });
  }

  // 获取表格数据
  @action
  getTableDatas = (params, columns_display, dataSource_display) => {
    Api.getTableDatas(params).then((result) => {
      result.columns[9].display = false;
      this.setTableDatats(columns_display, dataSource_display, result);
    });
  }

  // 处理表格数据
  @action
  setTableDatats = (columns_display, dataSource_display, result) => {
    // 处理列数据
    result.columns.forEach((element) => {
      if (element.display && element.display == 'true') {
        columns_display.push(element);
      }
    });
    // 处理行数据
    result.datas.forEach((datas, ids) => {
      let dataList = {};
      columns_display.forEach((element) => {
        let key = `${element.dataIndex}span`;
        dataList[element.dataIndex] = datas[key];
      });
      dataList.key = ids;
      dataList.docReceiveInfoIdspan = datas.docReceiveInfoIdspan;
      dataList.documentId = datas.docId;
      dataSource_display.push(dataList);
    });
    this.columns = columns_display;
    this.dataSource = dataSource_display;
  }

  // 获取表格数据条数
  @action
  getTableCounts = (params) => {
    Api.getTableCounts(params).then((data) => {
      this.setCount(data.count);
    });
  }

  @action
  setCount = (count) => {
    this.count = count;
  }

  // 获取高级搜索列表
  @action
  getTabFormData = () => {
    Api.getTabFormData().then((data) => {
      this.searchForm.setCondition(data.dealDateItems);
    });
  }

  @action
  changeShowSize = (current, pageSize) => {
    this.current = current;
    this.pageSize = pageSize;
    // this.doSearch(current,pageSize);
  }

  // 高级搜索
  @action.bound
  doSearch = () => {
    this.setShowSearchAd(false);
    let param = this.searchForm.getFormParams();
    this.loading = true;
    API_SEARCH_REQ.getSearchReqList(param).then(() => {
      this.getTableDatas(params);
      this.loading = false;
    });
  }
}
